import React, { Component } from "react";
import PropTypes from "prop-types";
import "./index.css";

export default class Item extends Component {
  state = {
    mouse: true,
  };

  static propTypes = {
  
    changeTodo: PropTypes.func.isRequired,
    deleteTodo: PropTypes.func.isRequired
  };

  handleCheck = (id) => {
    return (event) => {
      this.props.changeTodo(id, event.target.checked);
    };
  };

  handleMouse = (falg) => {
    return () => {
      this.setState({ mouse: falg });
    };
  };


  deleteItem=(id)=>{
    return ()=>{
      if(window.confirm('确定删除吗?')){
        this.props.deleteTodo(id);
      }

      console.log("通知删除"+id);
    }
  }

  render() {
    const { id, name, done } = this.props;
    const { mouse } = this.state;

    return (
      <li
        style={{ backgroundColor: mouse ? "#ffffff" : "#ddd" }}
        onMouseLeave={this.handleMouse(true)}
        onMouseEnter={this.handleMouse(false)}
      >
        <label>
          <input
            type="checkbox"
            onChange={this.handleCheck(id)}
            checked={done}
          />
          <span>{name}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? "none" : "block" }}
          onClick={this.deleteItem(id)}
        >
          删除
        </button>
      </li>
    );
  }
}
